-- UIEggs
-- create by zouyb
-- 处理彩蛋相关UI

-- UIEggs
UIEggs = class("UIEggs", function()
    return cc.Node:create();
end);

function UIEggs.create()
    return UIEggs.new();
end

-- 构造函数
function UIEggs:ctor()
    -- 注册彩蛋信息更新的回调函数
    EventMgr.register("UIEggs", event.EGGS_INFO_UPDATED, function()
        -- 如果当前处于迷宫中，这里不处理，迷宫出来时，会重新显示彩蛋
        if UIMgr.getCurrentScene():isOpen("UIDungeonMain") then
            return;
        end

        self:displayEggs();
    end);

    -- 注册彩蛋信息更新的回调函数
    EventMgr.register("UIEggs", event.EGG_BONUS, function(params)
        local bonusList = {};
        local bonus = params.bonus;
        local id = params.id;

        for _, b in pairs(bonus) do
            table.insert(bonusList, b);
        end

        local disChild = self.disEggNodeList[id];
        local child = self.eggNodeList[id];
        local disOffset = EggsM.query(id, "disappear_effect_offset");
        local effectId = EggsM.query(id, "disappear_effect_id");
        local scale    = EggsM.query(id, "disappear_effect_scale") or 1;
        local frameIndex = EggsM.query(id, "disappear_frame");
        self.isPlayEffect = true;
        disChild:setVisible(true);
        -- 播放消失效果
        local function frameEventFunc(frame)
            if frame.getEvent and frame:getEvent() == "pilor" then
                local name = frame:getNode():getName();
                local index = frame:getFrameIndex();

                -- 消失光效起始帧的时候，去掉待机动画
                if index == frameIndex.x then
                    -- child重新获取下
                    local child = self.eggNodeList[id];
                    if child ~= nil then
                        child:removeAllChildren();
                    end
                end

                -- 结束帧时跳出提示
                if index == frameIndex.y then
                    -- 如果有奖励，则需要提示获得奖励
                    if bonusList ~= nil and #bonusList > 0 then
                        showBonusNotifyForm(getLocStr("achive_award"), bonusList);
                    end
                    self.isPlayEffect = false;

                    -- 重刷一次
                    self:displayEggs();
                end
            end
        end

        playEffect(disChild, effectId, disOffset.x, disOffset.y, frameEventFunc, scale);
    end);

    -- 析构清理
    self:registerScriptHandler(function(event)
        if event == "exit" then
            EventMgr.removeAll("UIEggs");
        end
    end)
end

-- 显示彩蛋
function UIEggs:displayEggs()
    local mainMap = UIMgr.getCurrentScene().mainMap;
    if mainMap == nil or mainMap.eggParentNode == nil or self.isPlayEffect then
        return;
    end

    -- 先删除之前的彩蛋
    mainMap.eggParentNode:removeAllChildren();

    print("remove All eggs")

    mainMap.eggParentNode:setLocalZOrder(10000);
    self.eggEffectNodes = {};
    self.disEggNodeList = {};
    self.eggNodeList = {};

    local eggList = EggsM.getEggsList();

    local node = cc.CSLoader:createNode("layout/egg/EggInfo.csb");
    local widget = replaceNodeWithWidget(node);

    -- 依次在地图上显示彩蛋
    for _, eggId in pairs(eggList) do
        local infoId = math.floor(eggId % 65536);
        local posId = math.floor(eggId / 65536);

        if posId > 0 and infoId > 0 then
            local idlePos      = EggsM.query(posId, "idle_effect_pos");
            local idleOffset   = EggsM.query(posId, "idle_effect_offset");
            local idleEffectId = EggsM.query(infoId, "idle_effect_id");
            local disPos       = EggsM.query(posId, "disappear_effect_pos");
            local clickSize    =  EggsM.query(infoId, "click_size");
            local clickTimes   = EggsM.query(infoId, "click_times");

            local child = widget:clone();
            local disChild = widget:clone();
            child:setCascadeColorEnabled(true);
            child:setPosition(idlePos);
            disChild:setPosition(disPos);
            disChild:setVisible(false);

            mainMap.eggParentNode:addChild(child);
            mainMap.eggParentNode:addChild(disChild);
            table.insert(self.eggEffectNodes, child);
            self.disEggNodeList[infoId] = disChild;
            self.eggNodeList[infoId]    = child;

            local panel = findChildByName(child, "panel");
            panel:setTag(eggId);
            panel:setContentSize(clickSize.x, clickSize.y);
                panel:addTouchEventListener(function(sender, eventType)
                if eventType == ccui.TouchEventType.ended then
                    local id = sender:getTag();
                    print("click id:" .. id);

                    -- 领取彩蛋奖励
                    Operation.cmd_take_egg_bonus(id);

                    if panel.clickTimes == nil then
                        panel.clickTimes = 0;
                    end

                    panel.clickTimes = panel.clickTimes + 1;

                    if panel.clickTimes >= clickTimes then
                        panel:setTouchEnabled(false);
                    end
                end
            end);

            for _,id in pairs(idleEffectId) do
                playEffect(child, id, idleOffset.x, idleOffset.y);
            end
        end
    end

    -- 如果此时地图不可见，暂停特效
    if not mainMap:isVisible() then
       self:stopEffect();
    end
end

-- 检查特效
function UIEggs:checkEffect()
    for i = 1, #self.eggEffectNodes do
        local node = self.eggEffectNodes[i];
        if not isEffectNodeVisible(node) then
            -- 特效节点不可见，暂停播放
            if not node.isPause then
                pauseNode(node);
                node.isPause = true;
            end
        else
            -- 可见，继续播放
            if node.isPause then
                resumeNode(node);
                node.isPause = false;
            end
        end
    end
end

-- 停止特效
function UIEggs:stopEffect()
    if not self.eggEffectNodes then
        return;
    end

    for i = 1, #self.eggEffectNodes do
        pauseNode(self.eggEffectNodes[i]);
    end
end

-- 继续特效
function UIEggs:resumeEffect()
    if not self.eggEffectNodes then
        return;
    end

    for i = 1, #self.eggEffectNodes do
        resumeNode(self.eggEffectNodes[i]);
    end
end

-- 释放特效
function UIEggs:releaseEffect()
    local mainMap = UIMgr.getCurrentScene().mainMap;
    mainMap.eggParentNode:stopAllActions();
    mainMap.eggParentNode:removeAllChildren();

    self.eggEffectNodes = {};
    self.disEggNodeList = {};
    self.eggNodeList = {};
end

